#!/usr/bin/env python
#coding=utf8

import unittest
import datetime

from former_project import models
from base import ModelStatus
from former_project.common_logic import json

class SomeModel(models.Model):
    comment_type = models.IntegerField('评论类型', max_length=11, db_index=True)
    status       = models.IntegerField('状态', max_length=11, db_index=True)
    username     = models.CharField('用户名', max_length=50, null=True)
    content      = models.TextField('评论内容', null=True)
    created_time = models.DateTimeField('创建时间', db_index=True)
    
    class Meta:
        db_table = 'comment'

class ModelTest(unittest.TestCase):
    
    def test_Meta(self):
        self.assertEqual(SomeModel.table_schema.table_name, 
                         SomeModel.Meta.db_table)
        
    def test_dirty_fields(self):
        obj = SomeModel()
        self.assertFalse(obj.dirty_fields)
        self.assertEqual(obj.dirty_fields, [])
        obj.status = 1
        self.assertTrue('status' in obj.dirty_fields)
        self.assertEqual(obj._field_status['status'], ModelStatus.Changed)
        obj = SomeModel(status=1)
        self.assertTrue('status' in obj.dirty_fields)
        self.assertEqual(obj._field_status['status'], ModelStatus.Inited)
        
    def test_get_primary_key(self):
        obj = SomeModel()
        self.assertEqual(obj.get_primary_key()[0][0], 'id')
        
    def test_crud(self):
        o1 = SomeModel(comment_type=1,
                       status=1, username='modeltester', 
                       content='content here', 
                       created_time=datetime.datetime.now())
        self.assertEqual(len(o1.dirty_fields), 5)
        for k, v in o1._field_status.items():
            self.assertEqual(v, ModelStatus.Inited)
        pks, vals = o1.get_primary_key(True)
        self.assertTrue('id' in pks)
        self.assertFalse(vals)
        # insert
        id = o1.save()
        self.assertFalse(o1.dirty_fields)
        self.assertEqual(o1.update(), 0)
        o1.content = 'content update'
        self.assertEqual(o1.update(), 1)
        
        # update
        o11 = SomeModel(id=id, status=2)
        self.assertEqual(o11.save(), 1)
        
        o2 = SomeModel.fetch_objects('where id=%s', (id,))[0]
        self.assertEqual(o2.id, id)
        self.assertEqual(o2.status, o11.status)
        self.assertEqual(o2.username, o1.username)
#        
        # delete
        o1.delete()
        rs = SomeModel.fetch_objects('where id=%s', (id,))
        self.assertFalse(rs)
#        
        o3 = SomeModel(comment_type=1,
                       status=1, username='modeltester', 
                       content='content here', 
                       created_time=datetime.datetime.now())
        o4 = SomeModel()
        o4.id = o3.save()
        o4.delete()
        rs = SomeModel.fetch_objects('where id=%s', (o4.id,))
        self.assertFalse(rs)
        
        o5 = SomeModel(comment_type=1,
                       status=1, username='modeltester', 
                       content='content here', 
                       created_time=datetime.datetime.now())
        id = o5.save()
        rs = SomeModel.fetch_objects('where id=%s', (id,), source_result=True)
        self.assertTrue(rs)
        SomeModel.delete_objects('where id=%s', (id,))
        rs = SomeModel.fetch_objects('where id=%s', (id,), source_result=True)
        self.assertFalse(rs)
        
    def test_from_dict(self):
        o = SomeModel.from_dict({'comment_type': 1, })
        self.assertTrue(isinstance(o, SomeModel))
        self.assertEqual(o.comment_type, 1)
        self.assertEqual(o.content, None)
        
        self.assertRaises(TypeError, SomeModel.from_dict, [])
        
    def test_to_json(self):
        o3 = SomeModel(comment_type=1,
                       status=1, username='modeltester', 
                       content='content here', 
                       created_time=datetime.datetime.now())
        o4 = SomeModel()
        o4.id = o3.save()
        
        rs = SomeModel.fetch_objects('where id=%s', (o4.id,))
        print json.dumps(rs), rs
        rs = SomeModel.fetch_objects('where id=%s', (o4.id,), 
                                     source_result=True)
        print json.dumps(rs), rs
        o4.delete()
        rs = SomeModel.fetch_objects('where id=%s', (o4.id,))
        self.assertFalse(rs)
        
        
if __name__ == '__main__':
    unittest.main()